﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using WorkOrder.Business;
using WorkOrder.Model;
using System.Drawing;

namespace WorkOrder
{
    public partial class addWorkOrderInfo : System.Web.UI.Page
    {
        Business_Table_Work_order b_t_order = new Business_Table_Work_order();
        Model_Table_Work_order model;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.location.href='../login.aspx';</script>");
            }
            
            if (!Page.IsPostBack)
            {
                ddlProduct.Items.Clear();
                ddlProduct.DataTextField = "product_name";
                ddlProduct.DataValueField = "product_id";
                ddlProduct.DataSource = b_t_order.GetProduct();
                ddlProduct.DataBind();
                ddlProduct.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;
                seleTown.Items.Clear();
                seleTown.DataTextField = "town_name";
                seleTown.DataValueField = "town_id";
                seleTown.DataSource = b_t_order.GetTown();
                seleTown.DataBind();
                seleTown.Items.Add(new ListItem("-- 请选择 --", "0"));
                seleTown.SelectedIndex = seleTown.Items.Count - 1;
                txtGroupcode.Text = "";
                seleArea.Value = "";
                seleOrderType.Value = "";
                txtOrderContent.Text = "";
                txtOrderNo.Value = "";
                txtClustering_client.Value = "";
            }
           
        }

        #region 处理光标行颜色事件
        protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor,this.style.background='#ffe3a0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#fff'");
            }
        }
        #endregion

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (b_t_order.IsExist(txtOrderNo.Value.Trim()))
            {
                Label1.Text = "* 单号已存在！";
            }
            else
            { Label1.Text = ""; }
        }

        private String HtmlCode(string TString)
        {
            if (TString != null)
            {
                TString = TString.Replace("\r", " <br/> ");
                TString = TString.Replace(" ", "&nbsp;");
                return TString;
            }
            return "空";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            if (hidID.Value == "")
            {
                if (!new Business_Table_Permission().HasInsert(Session["UserID"].ToString(), "Table_Work_order"))
                {
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "提示信息：您没有新增工单信息的权限！";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您没有新增工单信息的权限！');window.close();</script>");
                    return;
                }
                if (txtOrderNo.Value.Trim() == "")
                    return;
                if (b_t_order.IsExist(txtOrderNo.Value.Trim()))
                {
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "提示信息：工单号为" + txtOrderNo.Value.Trim() + "的记录已经存在，请重新输入！";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('提示信息：工单号为" + txtOrderNo.Value.Trim() + "的记录已经存在，请重新输入！');window.close();</script>");
                    return;
                }
                if (hidTown.Value == "") hidTown.Value = "NULL";
                string order_id = Guid.NewGuid().ToString();

                model = new Model_Table_Work_order();
                model.Order_id = order_id;
                model.EntryPerson = Session["UserID"].ToString();
                model.GroupCode = Server.HtmlEncode(txtGroupcode.Text.Trim());
                model.OrderContent = Server.HtmlEncode(txtOrderContent.Text.Trim());
                model.Order_number = Server.HtmlEncode(txtOrderNo.Value.Trim());
                model.Order_type_id = seleOrderType.Value;
                model.Product_id = ddlProduct.SelectedValue;
                model.Remark = Server.HtmlEncode(txtRemark.Text.Trim());
                model.Town_id = hidTown.Value;
                model.Area_id = seleArea.Value;
                model.AccessMode = seleAccess.Value;
                model.CustomerCode = txtCustomerCode.Text.Trim();
                model.ClusteringClient = txtClustering_client.Value.Trim();
                if (b_t_order.Insert(model))
                {
                    hidID.Value = model.Order_id;
                    seleArea.Value = "";
                    seleOrderType.Value = "0";
                    txtOrderContent.Text = "";
                    txtOrderNo.Value = "";

                    hidTown.Value = "";
                    ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;
                    txtGroupcode.Text = "";
                    seleTown.Value = "0";
                    txtRemark.Text = "";


                    lblMsg.ForeColor = Color.DarkGreen;
                    lblMsg.Text = "提示信息：成功保存一条记录！";


                    lblOrderNo.Text = model.Order_number;
                    lblProduct.Text = b_t_order.GetProductByID(model.Product_id).Product_name;
                    lblOrderType.Text = b_t_order.GetOrderTypeByID(model.Order_type_id).Order_type;
                    lblArea.Text = b_t_order.GetAreaByID(model.Area_id).Area_name;
                    lblTown.Text = b_t_order.GetTownByID(model.Town_id).Town_name;
                    lblTitle.Text = model.Title;
                    lblGroupCode.Text = model.GroupCode;
                    lblCreator.Text = b_t_order.GetUserByID(model.EntryPerson).Remark;
                    lblCreateTime.Text = model.CheckInTime;
                    lblModify_content.Text = model.OrderContent;
                    lblPtime.Text = model.TaskTime;
                    lblRemark.Text = model.Remark;
                    lblClustering_client.Text = model.ClusteringClient;
                    lblFinishTime.Text = "";
                    lblAnalysis_time.Text = model.Analysis_time;
                    lblAudit_time.Text = model.Audit_time;
                    lblConfig_time.Text = model.Config_time;
                    lblCustomerCode.Text = model.CustomerCode;
                    lblConfirm_time.Text = model.Confirm_time;
                    DataList1.DataSource = b_t_order.GetException(model.Order_id);
                    DataList1.DataBind();
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "windref", "<script type='text/javascript' defer='defer'>ShowInfo();</script>");
                }
            }
            else
            {
                freshData();
            }
        }

        private void freshData()
        {
            if (hidID.Value != "")
            {
                model = b_t_order.GetData(hidID.Value);

                lblOrderNo.Text = model.Order_number;
                lblProduct.Text = b_t_order.GetProductByID(model.Product_id).Product_name;
                lblOrderType.Text = b_t_order.GetOrderTypeByID(model.Order_type_id).Order_type;
                lblArea.Text = b_t_order.GetAreaByID(model.Area_id).Area_name;
                lblTown.Text = b_t_order.GetTownByID(model.Town_id).Town_name;
                lblTitle.Text = model.Title;
                lblGroupCode.Text = model.GroupCode;
                lblCreator.Text = b_t_order.GetUserByID(model.EntryPerson).Remark;
                lblCreateTime.Text = model.CheckInTime;
                lblModify_content.Text = model.OrderContent;
                lblRemark.Text = model.Remark;
                lblClustering_client.Text = model.ClusteringClient;
                lblIsBack.Text = model.IsBack ? "是" : "否";
                lblAnalysis_time.Text = model.Analysis_time;
                lblAudit_time.Text = model.Audit_time;
                lblConfig_time.Text = model.Config_time;
                lblPtime.Text = model.TaskTime;
                lblConfirm_time.Text = model.Confirm_time;
                lblAccessMode.Text = model.AccessMode;
                lblCustomerCode.Text = model.CustomerCode;
                lblFinishTime.Text = model.FinishTime;
                DataList1.DataSource = b_t_order.GetException(model.Order_id);
                DataList1.DataBind();

                ClientScript.RegisterClientScriptBlock(this.GetType(), "wndref",
                                                       "<script type='text/javascript' defer='defer'>ShowInfo();</script>");
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            freshData();
        }
    }
}
